<?php

namespace app\admin\controller\old;

use app\common\controller\Backend;
use think\Config;
use think\Config as thconfig;
use think\Db;
use think\exception\DbException;

/**
 * 旧系统服务单
 *
 * @icon fa fa-circle-o
 */
class One extends Backend
{

    /**
     * One模型对象
     * @var \app\admin\model\old\One
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\old\One;
        $this->view->assign("statusList", $this->model->getStatusList());
        $site = thconfig::get('site');
        $this->view->assign('chexing', $site['chexing']);
        $this->view->assign('xingzhi', $site['xingzhi']);
        $admin = $this->auth->getUserInfo();
        $rule = Db::name('auth_group_access')->alias('a')->join('auth_group b','a.group_id = b.id')->where('a.uid',$admin['id'])->field('b.rules,a.group_id')->find();
        $rules = $rule['rules'];
        if($rules == '*'){
            $rules = Db::name('auth_rule')->column('id');
        }else{
            $rules = explode(',',$rule['rules']);
        }
        $this->rule = $rules;
        $this->assignconfig('rule', $rules);
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(["zuzhi"])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $row) {
                $row['sign'] = 0;
                $sign = Db::name('sign_old')->where('orderid',$row['id'])->find();
                if($sign){
                    $row['sign'] = 1;
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    public function getSubs1($categorys, $catId = 0, $level = 1)
    {
        $ids = array(); // 用于存储所有 id
        foreach ($categorys as $item) {
            if ($item['pid'] == $catId) {
                $ids[] = $item['id']; // 收集当前 id
                // 递归调用，确保总是返回数组
                $childIds = $this->getSubs1($categorys, $item['id'], $level + 1);
                if (is_array($childIds)) {
                    $ids = array_merge($ids, $childIds);
                }
            }
        }
        return $ids; // 返回 id 数组
    }

    public function chaxun(){
        $title = input('get.title');
        $url  = "http://dadi.guyuzixun.com/index/index/search_car?title={$title}";
        $result = sendCmd($url);
        $admin = $this->auth->getUserInfo();
        if($result['status'] == 1 && count($result['data']) > 0){
            $data = $result['data'][0];
            $info = $this->model->where(['car_vin'=>$data['car_vin'],'plan_strattime'=>$data['plan_strattime'],'plan_endtime'=>$data['plan_endtime']])->find();
            if($info){
                $this->success('','',['id'=>$info['id']]);
            }
            $data['yw_belonger'] = $data['yw_belonger'] ? $data['yw_belonger'] : $admin['username'];
            unset($data['id']);
            $data['yw_class'] = 1;
            $result = $this->model->allowField(true)->save($data);
            if($result){
                $this->success('','',['id'=>$this->model->id]);
            }else{
                $this->error('报错了');
            }
        }else{
            $this->error('没有查到信息');
        }
    }

    /**
     * 浏览
     * @param $ids
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function details($ids = null){
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
            $this->error(__('You have no permission'));
        }
        $row['car_register'] = date('Y-m-d',$row['car_register']);
        $row['plan_strattime'] = date('Y-m-d H:i:s',$row['plan_strattime']);
        $row['plan_endtime'] = date('Y-m-d H:i:s',$row['plan_endtime']);
        $row['yw_class'] = Db::name('zuzhi_old')->where('id',$row['yw_class'])->value('name');
        $row['dianwang'] = $row['ziran'];
        $row['dw_tong'] = $row['zi_tong'];
        $row['dw_biao'] = $row['zi_biao'];
        $row['dw_zhe'] = $row['zi_zhe'];
        $row['dw_ying'] = $row['zi_ying'];
        $row['dw_ji'] = $row['zi_buji'];
        $this->view->assign('row', $row);
        $this->assign('file', []);
        $this->assign('yijian',[]);
        return $this->view->fetch();
    }
    /**
     * 电子服务单
     * @return mixed|void
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public  function dian($ids = null){
        $id = $ids;
        if(empty($id)){
            $this->error('请先选择要生成服务单的对象');
        }else{
            $res = Db::name('list_one_old')->where('id',$id)->find();
            $site = Config::get('site');
            $res['car_use_nature_str'] = array_flip($site['xingzhi'])[$res['car_use_nature']];
            $tc_list = array();
            $k=0;
            $line = 8;
            if($res['main_standard']){
                $tc_list[$k]['tczl']='机动车车损损失';
                $tc_list[$k]['bjmp'] = $res['main_franchise'];
                $tc_list[$k]['money']= $res['main_money'];
                $tc_list[$k]['tcf']= $res['main_submit'];
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = round(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['main_standard'],2),100,4),2);
//                }

                $k=$k+1;
            }
            if($res['main_biao']){
                $tc_list[$k]['tczl']='机动车第三者责任';
                $tc_list[$k]['bjmp'] = $res['main_ji'];
                $tc_list[$k]['money']= $res['main_santong'];
                $tc_list[$k]['tcf']= $res['main_yingjiao'];
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = round(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['main_biao'],2),100,4),2);
//                }
                $k=$k+1;
            }
            if($res['dao_biao']){
                $tc_list[$k]['tczl']='医保外用药';
                $tc_list[$k]['bjmp'] = $res['dao_ji'];
                $tc_list[$k]['money']= $res['dao_tong'];
                $tc_list[$k]['tcf']= $res['dao_ying'];
                $k=$k+1;
            }
            if($res['siji_biao']){
                $tc_list[$k]['tczl']='机动车车上人员-司机 *1';
                $tc_list[$k]['bjmp'] = $res['siji_ji'];
                $tc_list[$k]['money']= $res['siji_tong'];
                $tc_list[$k]['tcf']= $res['siji_ying'];
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = round(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['siji_biao'],2),100,4),2);
//                }
                $k=$k+1;
            }
            if($res['ck_biao']){
                $tc_list[$k]['tczl']='机动车车上人员-乘客 *' . (string)($res['car_carry_peo'] - 1);
                $tc_list[$k]['bjmp'] = $res['ck_ji'];
                $tc_list[$k]['money']= $res['ck_tong'];
                $tc_list[$k]['tcf']= $res['ck_ying'];
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = round(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['ck_biao'],2),100,4),2);
//                }
                $k=$k+1;
            }
            $res['dianwang'] = $res['ziran'];
            $res['dw_tong'] = $res['zi_tong'];
            $res['dw_biao'] = $res['zi_biao'];
            $res['dw_zhe'] = $res['zi_zhe'];
            $res['dw_ying'] = $res['zi_ying'];
            $res['dw_ji'] = $res['zi_buji'];
            if($res['dw_biao']){
                $tc_list[$k]['tczl']='外部电网故障损失';
                $tc_list[$k]['bjmp'] = $res['dw_ji'];
                $tc_list[$k]['money']= $res['dw_tong'];
                $tc_list[$k]['tcf']= $res['dw_ying'];
                $k=$k+1;
            }
            $line = $line - $k;
            $this->assign('tc_list',$tc_list);
            $this->assign('line',$line);
//            if($res['kehu_sumdiscount'] != $res['main_sumdiscount']){
//                $main_sum = 0;
//                if($res['main_name'] == 1){
//                    $main_sum = bcadd($main_sum,round(bcmul(floatval($res['main_standard']),bcdiv($res['kehu_sumdiscount'],100,2),4),2),2);
//                }
//                if($res['main_disan'] == 1){
//                    $main_sum = bcadd($main_sum,round(bcmul(floatval($res['main_biao']),bcdiv($res['kehu_sumdiscount'],100,2),4),2),2);
//                }
//
//                if($res['siji'] == 1){
//                    $main_sum = bcadd($main_sum,round(bcmul(floatval($res['siji_biao']),bcdiv($res['kehu_sumdiscount'],100,2),4),2),2);
//                }
//
//                if($res['chengke'] == 1){
//                    $main_sum = bcadd($main_sum,round(bcmul(floatval($res['ck_biao']),bcdiv($res['kehu_sumdiscount'],100,2),4),2),2);
//                }
//
//                if($res['ziran'] == 1){
//                    $main_sum = bcadd($main_sum,round(bcmul(floatval($res['zi_biao']),bcdiv($res['kehu_sumdiscount'],100,2),4),2),2);
//                }
//                if($res['dao_name'] == 1){
//                    $main_sum = bcadd($main_sum,$res['dao_ying'],2);
//                }
//                $res['main_free'] = $main_sum;
//            }

            $this->assign('res',$res);
            $sign = Db::name('sign_old')->where('orderid',$id)->find();
            $this->assign('sign',$sign);
            $tiaokuan = db('tiaokuan')->order('id desc')->where(['gs_type'=>1])->limit(1)->find();
            $this->assign('tiaokuan',$tiaokuan);
            // 临时关闭当前模板的布局功能
            $this->view->engine->layout(false);
            return $this->fetch('dian');
        }
    }

}
